//
// Created by xili on 2024/7/31 10:48.
// Go big or go home.
//
#include <vector>
#include <unordered_set>

using namespace std;

struct ListNode {
    int val;
    ListNode *next;

    ListNode() : val(0), next(nullptr) {}

    ListNode(int x) : val(x), next(nullptr) {}

    ListNode(int x, ListNode *next) : val(x), next(next) {}
};

class Solution {
public:
    int numComponents(ListNode *head, vector<int> &nums) {
        unordered_set<int> s(nums.begin(), nums.end());
        int r = 1;
        int ans = 0;
        while (head) {
            if (s.count(head->val)) {
                ans += r;
                r = 0;
            } else {
                r = 1;
            }
            head = head->next;
        }
        return ans;
    }
};